package com.ai.bitcoin.controller;

import com.ai.bitcoin.common.resp.ResponseEntity;
import com.ai.bitcoin.constant.CacheConstant;
import com.ai.bitcoin.helper.GetQrcodeUrlHelper;
import com.ai.bitcoin.jpa.dao.BccAnnoInfoCrawlDao;
import com.ai.bitcoin.jpa.dao.BccBasicInfoDao;
import com.ai.bitcoin.jpa.entity.BccAnnoInfoCrawl;
import com.ai.bitcoin.jpa.entity.BccBasicInfo;
import com.ai.bitcoin.jpa.entity.BccNews;
import com.ai.bitcoin.jpa.service.BccNewsService;
import com.ai.bitcoin.redis.service.BccRedisService;
import com.ai.bitcoin.vo.Price2BccVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: ${description}
 * @author: gulj
 * @create: 2018-06-25 23:01
 **/
@RequestMapping("/info")
@Slf4j
@RestController
public class PriceYuqingGongGaoController {


    @Autowired
    private BccRedisService bccRedisService;

    @Autowired
    private BccNewsService bccNewsService;

    @Autowired
    private BccAnnoInfoCrawlDao bccAnnoInfoCrawlDao;

    @Autowired
    private BccBasicInfoDao basicInfoDao;


    @Autowired
    private GetQrcodeUrlHelper getQrcodeUrlHelper;

    /**
     * 获取 舆情（1），公告（2），价格（3）信息
     *
     * @param type
     * @param id
     * @return
     */
    @GetMapping("/{bccId}/{type}/{id}/{openId}")
    @Cacheable("articleInfo")
    public ResponseEntity info(@PathVariable("bccId") String bccId, @PathVariable("type") String type, @PathVariable("id") String id, @PathVariable String openId) {
        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(bccId) || StringUtils.isEmpty(id) || StringUtils.isEmpty(openId)) {
            return new ResponseEntity(-1, "params is not valid", null);
        }

        // 舆情
        if (type.equals("1")) {
            // 从数据库中重新获取数据
            BccNews bccNews = bccNewsService.queryByNewsId(id);
            if (null != bccNews) {
                String bccNameEn = getBccNameEn(bccId);
                bccNews.setBccNameEn(bccNameEn);
                // 重新放入缓存
                try {
                    // 二维码链接
                    bccNews.setQrcodeUrl(getQrcodeUrlHelper.getQrcodeUrl(bccId, openId));
                    bccRedisService.cache2SingleBccNew(bccId, id, bccNews);
                } catch (Exception e) {
                    log.error("【获取 舆情（1），公告（2），价格（3）信息——舆情】舆情信息，重新放入缓存失败");
                }
                return new ResponseEntity(200, "success", bccNews);
            }
            else {
                return new ResponseEntity(-1, "没有查询到舆情任何信息", null);
            }
        }
        // 公告
        if (type.equals("2")) {
            BccAnnoInfoCrawl singleBccAnnoInfo;
            // 从数据库中重新获取数据
            singleBccAnnoInfo = bccAnnoInfoCrawlDao.findByAnnoId(id);
            if (singleBccAnnoInfo != null) {
                String bccNameEn = getBccNameEn(bccId);
                singleBccAnnoInfo.setBccNameEn(bccNameEn);
                // 重新放入缓存
                try {
                    // 二维码链接
                    singleBccAnnoInfo.setQrcodeUrl(getQrcodeUrlHelper.getQrcodeUrl(bccId, openId));

                    bccRedisService.cache2SingleBccAnnoInfo(bccId, id, singleBccAnnoInfo);
                } catch (Exception e) {
                    log.error("【获取 舆情（1），公告（2），价格（3）信息——公告】公告信息，重新放入缓存失败");
                }
                return new ResponseEntity(200, "success", singleBccAnnoInfo);
            }
            else {
                return new ResponseEntity(-1, "没有查询到公告任何信息", null);
            }
        }

        // 价格
        if (type.equals("3")) {
            String key = CacheConstant.SINGLE_PRICE_INFO + bccId;
            Price2BccVo singlePrice2BccVo = bccRedisService.getSinglePrice2BccVo(key);
            if (singlePrice2BccVo == null) {
                return new ResponseEntity(-1, "没有获取到价格信息", null);
            }
            else {
                // 二维码链接
                singlePrice2BccVo.setQrcodeUrl(getQrcodeUrlHelper.getQrcodeUrl(bccId, openId));
                return new ResponseEntity(200, "success", singlePrice2BccVo);
            }
        }

        return new ResponseEntity(-1, "没有匹配到任何类型报告信息", null);
    }


    private String getBccNameEn(String bccId) {
        BccBasicInfo basicInfo = basicInfoDao.findByBccId(bccId);
        if (basicInfo != null) {
            return basicInfo.getNameEn();
        }
        return null;
    }
}
